/*
 * Copyright 2010 Kevin L'Huillier <klhuillier@gmail.com>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.lhuillier.pwsafe.model;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.lhuillier.pwsafe.Version;


/**
 * A single collection of headers and records stored together.
 * <p>
 * Generally, PasswordSafe databases are saved in a file ending in .psafe3.
 * This type contains all the persistent values contained in a DB. That is,
 * all the headers and records, but not the tag, salt, keys, and HMAC.
 */
public class Database {
    private Short version;
    private final UUID uuid;
    private Preferences preferences = new Preferences();
    private String treeDisplay;
    private Date lastSaveTime;
    private String lastSavePerson;
    private String lastSaveApp;
    private String lastSaveUser;
    private String lastSaveHost;
    private String dbName;
    private String dbDescription;
    private HashMap<Byte, RawField> unknownFields =
            new HashMap<Byte, RawField>();
    private final Set<Record> records = new HashSet<Record>();

    /**
     * Creates a database with a new ID.
     */
    public Database() {
        uuid = UUID.randomUUID();
        version = Version.DB_IMPL;
    }

    /**
     * @param uuid the ID of the database
     * @throws NullPointerException if uuid is null
     */
    public Database(UUID uuid) {
        if (uuid == null) {
            throw new NullPointerException("uuid");
        }
        this.uuid = uuid;
    }

    public Set<Record> getRecords() {
        return Collections.unmodifiableSet(records);
    }

    public void addRecord(Record record) {
        records.add(record);
    }

    public void removeRecord(Record record) {
        records.remove(record);
    }

    public Short getVersion() {
        return version;
    }

    public void setVersion(Short version) {
        this.version = version;
    }

    public UUID getUuid() {
        return uuid;
    }

    public Preferences getPreferences() {
        return preferences;
    }
    
    public PwPolicy getPasswordPolicy() {
        return new DbPwPolicy(preferences);
    }

    /**
     * A string of 0s and 1s indicating expanded state of the tree.
     */
    // TODO: This probably should be replaced with a full TreeDisplay type 
    public String getTreeDisplay() {
        return treeDisplay;
    }

    /**
     * A string of 0s and 1s indicating expanded state of the tree.
     */
    public void setTreeDisplay(String treeDisplay) {
        this.treeDisplay = treeDisplay;
    }

    public Date getLastSaveTime() {
        return lastSaveTime;
    }

    public void setLastSaveTime(Date lastSaveTime) {
        this.lastSaveTime = lastSaveTime;
    }

    public String getLastSavePerson() {
        return lastSavePerson;
    }

    public void setLastSavePerson(String lastSavePerson) {
        this.lastSavePerson = lastSavePerson;
    }

    public String getLastSaveApp() {
        return lastSaveApp;
    }

    public void setLastSaveApp(String lastSaveApp) {
        this.lastSaveApp = lastSaveApp;
    }

    public String getLastSaveUser() {
        return lastSaveUser;
    }

    public void setLastSaveUser(String lastSaveUser) {
        this.lastSaveUser = lastSaveUser;
    }

    public String getLastSaveHost() {
        return lastSaveHost;
    }

    public void setLastSaveHost(String lastSaveHost) {
        this.lastSaveHost = lastSaveHost;
    }

    public String getDbName() {
        return dbName;
    }

    public void setDbName(String dbName) {
        this.dbName = dbName;
    }

    public String getDbDescription() {
        return dbDescription;
    }

    public void setDbDescription(String dbDescription) {
        this.dbDescription = dbDescription;
    }

    public List<RawField> getUnknownFields() {
        ArrayList<RawField> result =
                new ArrayList<RawField>(unknownFields.values());
        Collections.sort(result, new FieldIdComparator());
        return result;
    }

    /**
     * If field is null, this removes any value for the given typeId, otherwise
     * it will replace any existing value or add a new value for the typeId.
     */
    public void setUnknownField(byte typeId, RawField field) {
        if (field == null) {
            unknownFields.remove(typeId);
        } else {
            unknownFields.put(typeId, field);
        }
    }

    public RawField getUnknownField(byte typeId) {
        return unknownFields.get(typeId);
    }
}
